import store from '@/store'

export default {
  methods: {
    hasPermission(value) {
      const roles = store.getters && store.getters.roles
      const authCodes = store.getters && store.getters.authCodes
      if (value && value instanceof Array && value.length > 0) {
        const permissionRoles = value

        //角色过滤，如admin
        var hasPermission = roles.some(role => {
          return permissionRoles.includes(role)
        })

        if (!hasPermission) {
          //菜单资源过滤
          hasPermission = authCodes.some(authCode => {
            return permissionRoles.includes(authCode)
          })
        }

        return hasPermission
      } else {
        throw new Error(`使用方式： v-if="hasPermission(['admin','editor'])"`)
      }
    }
  }
}
